package fx.utils;

import cn.hutool.core.codec.BCD;
import cn.hutool.crypto.digest.SM3;

import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;

/**
 * 统一加密类
 */
public class EncryptUtil {
    //AES加密 +MD5验签(默认)
    public static final String AES = "0";
    //国密SM4加密 + SM3验签
    public static final String SMS = "1";

    /**
     * <desc>
     * 解密接口
     * </desc>
     *
     * @param sSrc
     * @param sKey
     * @param type 加密方式
     * @return java.lang.String
     * @author maju
     * @createDate 2021/5/14
     */

    public static String decrypt(String sSrc, String sKey, String type) throws Exception {
        if (SMS.equals(type)) {
            return SMEncryptUtil.decryptStr(sSrc, sKey);
        }
        return DesEncryptUtil.aesDecrypt(sSrc, sKey);
    }

    /**
     * <desc>
     * 加密接口
     * </desc>
     *
     * @param sSrc
     * @param sKey
     * @param type 加密方式
     * @return java.lang.String
     * @author maju
     * @createDate 2021/5/14
     */

    public static String encrypt(String sSrc, String sKey, String type) throws Exception {
        if (SMS.equals(type)) {
            return SMEncryptUtil.encryptHex(sSrc, sKey);
        }
        return DesEncryptUtil.aesEncryptSDK(sSrc, sKey);
    }

    /**
     * <desc>
     * sdkV3版本验签方法
     * </desc>
     *
     * @param params
     * @param appSign
     * @param appsecret
     * @return
     */
    public static boolean checkSDKV3Sign(Map<String, Object> params, String appsecret, String appSign, String type, String... values) {
        if (SMS.equals(type)) {
            return SMEncryptUtil.checkSDKV3Sign(params, appsecret, appSign, values);
        }
        return Md5EncryptUtil.checkSDKV3Sign(params, appsecret, appSign, values);
    }

    /**
     * <desc>
     * sdkv3计算sign
     * </desc>
     *
     * @param params
     * @param appsecret
     * @return
     */
    public static String getSDKV3Sign(Map<String, Object> params, String appsecret, String type, String... values) {
        if (SMS.equals(type)) {
            return SMEncryptUtil.getSDKV3Sign(params, appsecret, values);
        }
        return Md5EncryptUtil.getSDKV3Sign(params, appsecret, values);
    }


    /**
     * <desc>
     * 返回加密内容sdkV3
     * </desc>
     *
     * @param appsecret 开发者用户密码
     * @return
     * @author Qiang.S
     * @createDate 2018/12/27
     */
    public static Map<String, Object> getEncryptResultSDKV3(Object result, String appsecret, String type) {
        if (SMS.equals(type)) {
            return SMEncryptUtil.getEncryptResultSDKV3(result, appsecret);
        }
        return DesEncryptUtil.getEncryptResultSDKV2(result, appsecret);
    }

    /**
     * <desc>
     * 通过项目id生成指定数据库秘钥
     * </desc>
     *
     * @param projectId
     * @return java.lang.String
     * @author maju
     * @createDate 2021/11/4
     */
    public static String getKeyByProjectId(String projectId) {
        String key = BCD.bcdToStr(projectId.getBytes(StandardCharsets.UTF_8));
        key = new SM3().digestHex(key);
        key = key.substring(0, 16);
        key = key.toUpperCase(Locale.ROOT);
        return key;
    }

    private static final String CERTIFICATE_PREFIX = "ITLONG";

    /**
     * <desc>
     * 通过项目id生成证书解密秘钥
     * </desc>
     *
     * @param projectId
     * @return java.lang.String
     * @author maju
     * @createDate 2021/11/4
     */
    public static String getCertificateKey(String projectId) {
        String key = BCD.bcdToStr(projectId.getBytes(StandardCharsets.UTF_8));
        key = new SM3().digestHex(CERTIFICATE_PREFIX + key);
        key = key.substring(0, 16);
        key = key.toUpperCase(Locale.ROOT);
        return key;
    }
}
